<template>
  <div class="app-container">
    <el-card class="content-edit">
      <div slot="header">添加机场</div>
      <el-form ref="form" :model="form" label-width="160px">
        <el-form-item label="机场名称">
          <el-input v-model="form['name']"/>
        </el-form-item>
        <el-form-item label="举牌服务">
          <div>
            <el-switch v-model="form['placards']"/>
          </div>
          <el-input :disabled="!form['placards']" v-model="form['price']" :min="0" type="number" style="width: 240px;">
            <span slot="append">元 / 次</span>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" :disabled="btnLoading" type="primary" @click="onSubmit">保存</el-button>
          <el-button @click="$router.back()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: 'PresetAirportAdd',
    data() {
      return {
        // 提交按钮点击事件
        btnLoading: false,
        form: {
          // 机场名称
          name: null,
          // 举牌价格
          price: null,
          // 举牌服务
          placards: false
        }
      }
    },
    mounted(){},
    methods: {
      // 提交
      onSubmit() {
        this.$refs.form.validate(valid => {
          if (valid) {
            if (parseInt(this.form['price']) < 0) {
              this.$message.error('举牌服务价不能小于0');
              return;
            }
            (async () => {
              const params = Object.assign({}, this.form);
              params['name'] = params['name'] && params['name'].replace(' ', '');
              params['placards'] = params['placards'] ? 2 : 1;
              const result = await this.$service.applicationAddAirport(params);
              if (result.code === 0) {
                this.$message.success('操作成功~');
                this.$router.back();
              } else {
                this.$message.error(result.msg);
              }
            })();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>
